import java.io.IOException;
import java.net.URL;
import java.nio.ByteBuffer;

import javax.media.opengl.GL;
import javax.media.opengl.glu.GLU;

import com.sun.opengl.util.BufferUtil;
import com.sun.opengl.util.texture.TextureData;
import com.sun.opengl.util.texture.TextureIO;

public class Mipmap
{
	private GL gl;
	private GLU glu;
	private int num_tex;
	private TextureData mipmapImageBuf[];
	ByteBuffer tmp512[];
	ByteBuffer tmp256[];
    ByteBuffer tmp128[];
    ByteBuffer tmp64[];
    ByteBuffer tmp32[];
    ByteBuffer tmp16[];
    ByteBuffer tmp8[];
    ByteBuffer tmp4[];
    ByteBuffer tmp2[];
    ByteBuffer tmp1[];
	//mipmap
 
  public Mipmap(GL gl_, GLU glu_, int num)
  {   
	  num_tex = num;
	  gl = gl_;
	  glu = glu_;
	  mipmapImageBuf= new TextureData[num_tex];
	  tmp512 = new ByteBuffer[num_tex];
	  tmp256 = new ByteBuffer[num_tex];
	  tmp128 = new ByteBuffer[num_tex];
	  tmp64 = new ByteBuffer[num_tex];
	  tmp32 = new ByteBuffer[num_tex];
	  tmp16 = new ByteBuffer[num_tex];
	  tmp8 = new ByteBuffer[num_tex];
	  tmp4 = new ByteBuffer[num_tex];
	  tmp2 = new ByteBuffer[num_tex];
	  tmp1 = new ByteBuffer[num_tex];
  }

  public void LoadImages(int i) {
	  	int[] g_nMaxAnisotropy = new int[1];
	    gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, 512, 512, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
		        tmp512[i]);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 1, GL.GL_RGBA, 256, 256, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
		        tmp256[i]);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 2, GL.GL_RGBA, 128,128, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
	    		tmp128[i]);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 3, GL.GL_RGBA, 64, 64, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
	    		tmp64[i]);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 4, GL.GL_RGBA, 32, 32, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
	    		tmp32[i]);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 5, GL.GL_RGBA, 16, 16, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
	    		tmp16[i]);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 6, GL.GL_RGBA, 8, 8, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
	    		tmp8[i]);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 7, GL.GL_RGBA, 4, 4, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
	    		tmp4[i]);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 8, GL.GL_RGBA, 2, 2, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
	    		tmp2[i]);
	    gl.glTexImage2D(GL.GL_TEXTURE_2D, 9, GL.GL_RGBA, 1, 1, 0, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE,
	    		tmp1[i]);

	    gl.glGetIntegerv(GL.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT,g_nMaxAnisotropy,0);
	    System.out.println(g_nMaxAnisotropy[0]);
	    gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAX_ANISOTROPY_EXT,g_nMaxAnisotropy[0]);
	    gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
	    gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
	    gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
	    gl.glTexParameterf(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER,
	        GL.GL_LINEAR_MIPMAP_LINEAR);
	    gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_DECAL);
	}
  
  public void makeImages(URL imageURL, int i)
  {
	tmp512[i] = BufferUtil.newByteBuffer(512 * 512 * 4);
	tmp256[i] = BufferUtil.newByteBuffer(256 * 256 * 4);
    tmp128[i] = BufferUtil.newByteBuffer(128 * 128 * 4);
    tmp64[i] = BufferUtil.newByteBuffer(64 * 64 * 4);
    tmp32[i] = BufferUtil.newByteBuffer(32 * 32 * 4);
    tmp16[i] = BufferUtil.newByteBuffer(16 * 16 * 4);
    tmp8[i] = BufferUtil.newByteBuffer(8 * 8 * 4);
    tmp4[i] = BufferUtil.newByteBuffer(4 * 4 * 4);
    tmp2[i] = BufferUtil.newByteBuffer(2 * 2 * 4);
    tmp1[i] = BufferUtil.newByteBuffer(1 * 1 * 4);
    try {
		mipmapImageBuf[i]= TextureIO.newTextureData(
				 imageURL.openStream(),false, "png");
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}       
    int width = mipmapImageBuf[i].getWidth(),height = mipmapImageBuf[i].getHeight();
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 512, 512, GL.GL_UNSIGNED_BYTE, tmp512[i]);
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 256, 256, GL.GL_UNSIGNED_BYTE, tmp256[i]);
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 128, 128, GL.GL_UNSIGNED_BYTE, tmp128[i]);
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 64, 64, GL.GL_UNSIGNED_BYTE, tmp64[i]);
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 32, 32, GL.GL_UNSIGNED_BYTE, tmp32[i]);
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 16, 16, GL.GL_UNSIGNED_BYTE, tmp16[i]);
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 8, 8, GL.GL_UNSIGNED_BYTE, tmp8[i]);
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 4, 4, GL.GL_UNSIGNED_BYTE, tmp4[i]);
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 2, 2, GL.GL_UNSIGNED_BYTE, tmp2[i]);
    glu.gluScaleImage(GL.GL_RGBA, width, height, GL.GL_UNSIGNED_BYTE, mipmapImageBuf[i].getBuffer(), 1, 1, GL.GL_UNSIGNED_BYTE, tmp1[i]);
  }
  
}